package com.hubei.car.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 1.根据IP获取地址信息
 * 119.123.69.157 广东省深圳市 电信
 * 2.根据头部String agent = httpServletRequest.getHeader("user‐agent");解析出浏览器
 */
public class WebUtil {


    //GET字符集设置处理
    public static Map<String, String> convertCharsetToUTF8(Map<String, String> searchMap) throws Exception {
        Iterator<Map.Entry<String, String>> entries = searchMap.entrySet().iterator();
        Map map = new HashMap();
        while (entries.hasNext()) {
            Map.Entry<String, String> entry = entries.next();
            map.put(new String(entry.getKey().getBytes("ISO8859-1"), "UTF-8"), new String(entry.getValue().getBytes("ISO8859-1"), "UTF-8"));
        }
        return map;
    }

    /**
     * String ip = httpServletRequest.getRemoteAddr(); //获取访问的ip
     * WebUtil.getCityByIP(ip);//获取城市信息
     * @param ip
     * @return
     * @throws JSONException
     */
    public static String getCityByIP(String ip) throws JSONException {
        if(ip.equals("0:0:0:0:0:0:0:1")){
            return "本地";
        }
        try {
            URL url = new URL("http://opendata.baidu.com/api.php?query=" + ip + "&co=&resource_id=6006&t=1433920989928&ie=utf8&oe=utf-8&format=json");
            URLConnection conn = url.openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line = null;
            StringBuffer result = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }
            reader.close();
            JSONObject jsStr = JSONObject.parseObject(result.toString());
            JSONArray jsData = (JSONArray) jsStr.get("data");
            JSONObject data = (JSONObject) jsData.get(0);//位置
            return (String) data.get("location");
        } catch (IOException e) {
            return "读取失败";
        }
    }



    /**
     * 浏览器类型
     * String agent = httpServletRequest.getHeader("user‐agent");
     * 获取到的信息如下：
     * Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like
     * Gecko) Chrome/71.0.3578.98 Safari/537.36.....
     * 我们没有必要存这么长的字符串，我们只需要存储一个浏览器名称就可以了，所以我们
     * 用到了WebUtil给我们提供的方法 getBrowserName( String agent )
     * @param agent
     * @return
     */
    public static String getBrowserName(String agent) {
		agent=agent.toLowerCase(); 
        if (agent.indexOf("msie 7") > 0) {
            return "ie7";
        } else if (agent.indexOf("msie 8") > 0) {
            return "ie8";
        } else if (agent.indexOf("msie 9") > 0) {
            return "ie9";
        } else if (agent.indexOf("msie 10") > 0) {
            return "ie10";
        } else if (agent.indexOf("msie") > 0) {
            return "ie";
        } else if (agent.indexOf("opera") > 0) {
            return "opera";
        } else if (agent.indexOf("chrome") > 0) {
            return "chrome";
        } else if (agent.indexOf("firefox") > 0) {
            return "firefox";
        } else if (agent.indexOf("webkit") > 0) {
            return "webkit";
        } else if (agent.indexOf("gecko") > 0 && agent.indexOf("rv:11") > 0) {
            return "ie11";
        } else {
            return "others";
        }
    }


    public static void main(String[] args) {
        String city= getCityByIP("119.123.69.157");
        System.out.println(city);

    }

}
